<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE >
<html style="font-size: 100px;">
<head>
    <base href="<%=basePath%>">

    <title>登录</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <%@include file="/login/common.jsp" %>
    <link rel="stylesheet" type="text/css" href="css/style.css" />
    <script type="text/javascript" src="<%=request.getContextPath()%>/js/auth.js"></script>
    <script type="text/javascript">
         $(function(){
             var firstLogin = localStorage.getItem("firstLogin");
             if(firstLogin != null && firstLogin == 'false'){
                 autoLogin();
             }
            var ua = window.navigator.userAgent.toLowerCase();
            console.log(ua);//mozilla/5.0 (iphone; cpu iphone os 9_1 like mac os x) applewebkit/601.1.46 (khtml, like gecko)version/9.0 mobile/13b143 safari/601.1
            if (ua.match(/MicroMessenger/i) == 'micromessenger') {
                return true;
            } else {
                return false;
            }
         })
        function doLogin(){
            var username = $.trim($("#username").val());
            if(username==""){
            myAlert("请输入用户名");
                return;
            }
            var password = $.trim($("#password").val());
            if(password == "" || password.length < 5){
                myAlert("请输入至少5位的密码");
                return;
            }
            var url = '${linkPath}smartfire/wx/login';
            $(".login-btn").html("登录中...");
            var loginAjax = $.ajax({
                type:"post",
                url:url,
                    data:{'username':username,'password':password},
                    dataType:"JSON",
                    timeout : 5000,
                    success:function(res){
                    if(res.success){
                        $(".login-btn").html("登录成功");
                        sessionStorage.setItem("token", res.obj.token);
                        sessionStorage.setItem("userId", res.obj.userInfo.id);
                        sessionStorage.setItem("orgId", res.obj.userInfo.orgId);
                        localStorage.setItem("user",JSON.stringify(res.obj.userInfo));
                        var loginInfo = '{"username": "'+username+'","password": "'+password+'"}';
                        localStorage.setItem("login",loginInfo);
                        localStorage.setItem("firstLogin",false);
                        //微信登录授权
                        var userId = res.obj.userInfo.id;
                        if(userId  == null){
                            myAlert("登录失败");
                        }else{
                            location.href = linkPath+"smartfire/wx/auth?userId=" + userId;
                        }
                    }else{
                        $(".login-btn").html("登录");
                        myAlert(res.msg);
                    }
                },
                complete : function(XMLHttpRequest,status){
                if(status=='timeout'){
                    loginAjax.abort();
                    myAlert("服务器繁忙");
                    $(".login-btn").html("登录");
                }
                },
                error:function(r){
                    myAlert("服务器繁忙");
                    $(".login-btn").html("登录");
                }
            });
        }
        function pwdVisible(obj){
            if($(obj).attr("src").indexOf("invisible") == -1){
                $(obj).attr("src","${basePath}images/login/login-pwd-invisible.png");
                $("#password").attr("type","password");
            }else{
                $(obj).attr("src","${basePath}images/login/login-pwd-visible.png");
                $("#password").attr("type","text");
            }
        }
        function checkLoginInput(){
            if($("#username").val().trim() != "" && $("#password").val().trim() != ""){
                $(".login-btn").removeClass("invalid");
            }else{
                $(".login-btn").addClass("invalid");
            }
        }
        $(function(){
            checkLoginInput();
            $("#username").keyup(function(){
                checkLoginInput();
            });
            $("#password").keyup(function(){
                checkLoginInput();
            });
        });
    </script>
</head>

<body>
<div class="whole">
    <div class="title">欢迎来到智慧消防平台</div>
    <div class="coordinates-icon">
        <div class="coordinates topAct">
            <img src="images/login/fire.png" />
        </div>
        <div class="circle-1-1 circle-show-2"></div>
        <div class="circle-2-2 circle-show-1"></div>
        <div class="circle-3-3 circle-show"></div>
    </div>
    <div class="login-form">
        <div class="user-name common-div">
            <span class="eamil-icon common-icon">
                <img src="images/login/name.png" />
            </span>
            <input type="text" name="username" id="username" value="" placeholder="用户名" />
        </div>
        <div class="user-pasw common-div">
            <span class="pasw-icon common-icon">
                <img src="images/login/key.png" />
            </span>
            <input type="password" name="password" id="password" value="" placeholder="******" />
        </div>
        <div class="login-btn common-div" onclick="doLogin()">登录</div>
    </div>
</div>
</body>
</html>